//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for CommentEditor.
	/// </summary>
	public class CommentEditor : BaseBlogPage
	{

		#region Member Variables

		protected LinkButton PublishButton;
        protected LinkButton UnPublishButton;

		protected TextBox ModeratedBy;
		protected Editor Comment;
		protected TextBox Url;
		protected TextBox UserName;
		protected TextBox Title;
        const string CommentSafeMode = "CommentSafeMode";

		private WeblogPost currentPost = null;

		#endregion

		
		override protected void OnInit(EventArgs e)
		{

            
			this.PublishButton.Click += new EventHandler(this.Save_Click);
            this.UnPublishButton.Click += new EventHandler(this.Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		

		private void Page_Load(object sender, EventArgs e)
		{

			int postid = -1;

			CSContext cntx = CSContext.Current;
			if(cntx.QueryString["cid"] != null)
			{
				try
				{
					postid = Int32.Parse(Context.Request.QueryString["cid"]);
				}
				catch{}
			}

			if(postid > 0)
			{
				cntx.PostID = postid;
				currentPost = WeblogPosts.GetPost(postid, true, false, false);
			}

            if ( !Page.IsPostBack ) 
            {
                PublishButton.Text = Components.ResourceManager.GetString( "FeedbackEditor_Save" );
                UnPublishButton.Text = Components.ResourceManager.GetString("FeedbackEditor_NotSaved");
                Comment.EnableHtmlModeEditing = true;


                Title.Text = Globals.HtmlDecode(currentPost.Subject);
                
                if(currentPost.GetBool(CommentSafeMode, true))
                {
                    Comment.Text = currentPost.FormattedBody;
                }
                else
                {
                    Comment.Text = currentPost.Body;
                }
                Url.Text = currentPost.TitleUrl;

                
				//if (currentPost.HasSubmittedUserName)
                UserName.Text =  currentPost.DisplayName;
                if(currentPost.AuthorID != Users.GetAnonymousUser().UserID)
                {
                    UserName.Attributes.Add("disabled","true");
                }
                
            }

			Head.AddTitle("Comment Editor", this.Context);
		}


		/// <summary>
		/// Provides a hook for custom logic in the skin file to easily save additional data to the post.
		/// </summary>
		protected void SaveAdditionalContent(WeblogPost post) 
		{
			// If the ExtendedAttribsToSave hashtable exists in HTPPContext,
			//  loop through each item and add them to the post's Extended Attributes
			Hashtable ExtendedAttribsToSave = CSContext.Current.Context.Items["ExtendedAttribsToSave"] as Hashtable;
			if (ExtendedAttribsToSave != null)
			{
				if (ExtendedAttribsToSave != null)
				{
					foreach (object key in ExtendedAttribsToSave.Keys)
					{
						post.SetExtendedAttribute(Convert.ToString(key), Convert.ToString(ExtendedAttribsToSave[key]));
					}
				}

			}
		}
	
	

		#region Button Click Handlers

		private void Save_Click(object sender, EventArgs e) 
		{
            LinkButton lb = sender as LinkButton;
            bool isApproved = lb.CommandArgument == "publish";

            // Retrieve post from database to edit
            CSContext cntx = CSContext.Current;	
            currentPost = WeblogPosts.GetPost(cntx.PostID, true, false, false);

            // Retrieve user from database so the current user won't override the original
            if (currentPost.User == null)
            {
                string username = Globals.IsNullorEmpty(currentPost.Username) ? "" : currentPost.Username;
                currentPost.User = Users.GetUser(currentPost.AuthorID, username, false, false);
            }


            currentPost.Subject = currentPost.Subject;

            if(currentPost.AuthorID == Users.GetAnonymousUser().UserID)
            {
                currentPost.SubmittedUserName = UserName.Text;
            }
            



            currentPost.Subject = Title.Text;

            currentPost.Body = Comment.Text;

            currentPost.TitleUrl = Url.Text;
            

			
                
            //currentPost.IsApproved = Approved.Checked;

            currentPost.PostType = PostContentType.HTML;
            currentPost.SetExtendedAttribute("ModeratedBy", cntx.User.Username);
            currentPost.IsApproved = isApproved;
             
            // Provide a hook for any child class to easily save stuff
            SaveAdditionalContent(currentPost);

            WeblogPosts.Update(currentPost, cntx.User.UserID);

			Modal.ClosePage(Page,"true") ;

		}


		#endregion

	}
}
